package com.tyqx.biz.controller;


import com.tyqx.bean.model.SubServerPlot;
import com.tyqx.bean.model.SubServerSendInfo;
import com.tyqx.service.SubServerInfoService;
import com.tyqx.service.SubServerPlotService;
import com.tyqx.service.SubServerSendInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequestMapping("/mainServer")
@Controller
public class ReceiveSubServiceSendDataController {

    private static final Logger logger = LoggerFactory.getLogger(ReceiveSubServiceSendDataController.class);


    @Resource
    private SubServerInfoService subServerInfoService;

    @Resource
    private SubServerSendInfoService subServerSendInfoService;

    @Resource
    private SubServerPlotService subServerPlotService;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");


    @RequestMapping(value = "/sendData",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String sendData(String data) {
        logger.info("data:" + data);
        try {
            if (null == data || "" == data) {
                logger.info("给主服务器发送的数据为空，请填写数据");
                return "给主服务器发送的数据为空，请填写数据";
            }

            JSONObject dataJsonObject = JSONObject.parseObject(data);

            String computerName = dataJsonObject.getString("computName");

            if (!subServerInfoService.selectSubServerInfoCountByComputName(computerName)) {
                logger.info("发送的数据computerName:"+computerName+"在数据库中找不到，请核对");
                return "发送的数据computerName:"+computerName+"在数据库中找不到，请核对";
            }

            SubServerSendInfo subServerSendInfo = new SubServerSendInfo();
            subServerSendInfo.setSubServerComputName(computerName);
            subServerSendInfo.setSubServerSendData(data);
            subServerSendInfo.setCreateDate(dateFormat.parse(dateFormat.format(new Date())));
            if(subServerSendInfoService.insert(subServerSendInfo)){
                logger.info(computerName+"向主服务器发送数据成功");
                return computerName+"向主服务器发送数据成功";
            }else{
                logger.error(computerName+"向主服务器发送数据失败");
                return computerName+"向主服务器发送数据失败";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "发生异常错误";
    }

    @RequestMapping(value = "/sendPlotNum",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String sendPlotNum(String data) {
        logger.info("data:" + data);
        try {
            if (null == data || "" == data) {
                logger.info("给主服务器发送的plotNum数据为空，请填写数据");
                return "给主服务器发送的plotNum数据为空，请填写数据";
            }

            JSONObject dataJsonObject = JSONObject.parseObject(data);

            String computerName = dataJsonObject.getString("computName");
            Integer plotNum = dataJsonObject.getInteger("plotNum");
            String produceDate = dataJsonObject.getString("produceDate");

            if (!subServerInfoService.selectSubServerInfoCountByComputName(computerName)) {
                logger.info("发送的数据(plot)computerName:"+computerName+"在数据库中找不到，请核对");
                return "发送的数据(plot)computerName:"+computerName+"在数据库中找不到，请核对";
            }

            SubServerPlot subServerPlot = new SubServerPlot();
            subServerPlot.setComputName(computerName);
            subServerPlot.setPlotNum(plotNum);
            subServerPlot.setProduceDate(dateFormat1.parse(produceDate));
            subServerPlot.setCreateDate(dateFormat.parse(dateFormat.format(new Date())));
            if(subServerPlotService.selectSubServerPlotByProduceDate(produceDate)>0){
                logger.info("computName:"+computerName + ",produceDate:"+produceDate+";当天的数据已经添加");
                return "computName:"+computerName + ",produceDate:"+produceDate+";当天的数据已经添加";
            }else {
                if (subServerPlotService.addSubServerPlot(subServerPlot)) {
                    logger.info(computerName + "获取plot数量成功");
                    return computerName + "获取plot数量成功";
                } else {
                    logger.error(computerName + "获取plot数量失败");
                    return computerName + "获取plot数量失败";
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "发生异常错误";
    }
}
